package cn.stronglink.asset.module.assetmanage.repair.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.plugins.Page;
import cn.stronglink.asset.model.Repair;
import cn.stronglink.asset.model.RepairDetail;
import cn.stronglink.asset.module.assetmanage.itasset.vo.ItAssetVo;
import cn.stronglink.asset.module.assetmanage.repair.service.RepairService;
import cn.stronglink.suite.core.base.AbstractController;
import cn.stronglink.suite.core.log.annotation.OperateLog;
import cn.stronglink.suite.core.log.annotation.OperateLog.OpType;
import cn.stronglink.suite.core.util.WebUtil;
/** 
* @author sunlei: 
* @version 创建时间：2017年11月13日 下午15:25:01 
* 类说明    设备维修控制器
*/
@Controller
@RequestMapping(value = "repair")
public class RepairController extends AbstractController {

	@Autowired
	private RepairService repairService;

	/**
	 * 查询所有维修单并分页
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "queryList")
	public ResponseEntity<ModelMap> queryList(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody Map<String, Object> params) {	
		boolean isAdmin = (boolean) request.getSession().getAttribute("isAdmin");
		if(!isAdmin) {
			params.put("operatorBy", WebUtil.getCurrentUser());
		}
		@SuppressWarnings("unchecked")
		Page<Repair> page = (Page<Repair>) super.getPage(params);
		Page<Repair> data = repairService.queryList(page,params);
		return setSuccessModelMap(modelMap, data);
	}
	
	/**
	 * 根据维修单Id查询巡检明细
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "qryById")
	public ResponseEntity<ModelMap> qryById(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody Long id) {	
		Repair data =repairService.qryById(id);
//		List<RepairDetail> detailVo = repairService.queryRepairDetialList(id);
//		modelMap.put("detailVo", detailVo);
		return setSuccessModelMap(modelMap, data);
	}
	
	/**
	 * 创建维修单
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param inventory
	 * @return
	 */
	@OperateLog(module = "维修单管理",desc="添加维修单", type = OpType.ADD)
	@RequestMapping(value = "create")
	public ResponseEntity<ModelMap> create(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Repair repair) {
		repairService.create(repair);
		return setSuccessModelMap(modelMap, null);
	}
	

	/**
	 * 编辑维修单
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param inventory
	 * @return
	 */
	@OperateLog(module = "维修单管理",desc="编辑维修单", type = OpType.UPDATE)
	@RequestMapping(value = "edit")
	public ResponseEntity<ModelMap> edit(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody  Repair repair) {
		repairService.edit(repair);
		return setSuccessModelMap(modelMap, null);
	}
	/**
	 * 删除维修单
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 */
	@OperateLog(module = "维修单管理",desc="删除维修单", type = OpType.DEL)
	@RequestMapping(value = "remove")
	public ResponseEntity<ModelMap> remove(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Long id) {
		repairService.remove(id);
		return setSuccessModelMap(modelMap, null);
	}
	/**
	 * 根据计划详情id获取维修单列表
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "qryRepairList")
	public ResponseEntity<ModelMap> qryPatrolList(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Map<String,Object> params) {
		@SuppressWarnings("unchecked")
		Page<Repair> page = (Page<Repair>) super.getPage(params);
		Page<Repair> data = repairService.queryList(page, params) ;
		return setSuccessModelMap(modelMap, data);
	}
	
	/**
	 * 通过维修id查询维修项详情列表
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param params
	 * @return
	 */
	@RequestMapping(value="queryRepairDetail")
	public ResponseEntity<ModelMap> queryDetailsList(ModelMap modelMap,HttpServletRequest request,
			HttpServletResponse response,@RequestBody Map<String,Object> params,long id){
		List<RepairDetail> data = repairService.queryRepairDetialList(id);
		return setSuccessModelMap(modelMap,data);
	}
	
	/**
	 * 确认维修
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param repair
	 * @return
	 */
	@OperateLog(module = "维修单管理",desc="确认维修单", type = OpType.UPDATE)
	@RequestMapping(value="doRepair")
	public ResponseEntity<ModelMap> doRepair(ModelMap modelMap,HttpServletRequest request,
			HttpServletResponse response,@RequestBody Repair repair){
		repairService.doRepair(repair);
		return setSuccessModelMap(modelMap,null);
	}
	
	/**
	 * 所在机房,设备类型,品牌,设备名称模糊查找资产
	 * @param modelMap
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "querAssetListByParam")
	public ResponseEntity<ModelMap> queryDetailList(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Map<String, Object> params) {	
		@SuppressWarnings("unchecked")
		Page<ItAssetVo> page = (Page<ItAssetVo>) super.getPage(params);
		Page<ItAssetVo> data = repairService.querAssetListByParam(page,params);
		return setSuccessModelMap(modelMap, data);
	}
}
